# -*- encoding: utf-8 -*-
"""
File Name: zabbix_problem_to_elastic
Description:
Author : 'lirui'
date: 2024/1/12
Contact: marjey_lee@163.com
Change Activity:
2024/1/12:
"""
import time
from threading import Thread

import ll
from projects.monitoring.common.utils.redis_problem_publishers_manager import RedisProblemsPublishersManager
from projects.monitoring.internet_zabbix_monitoring.services.load_problems_from_zabbix import LoadProblemsFromZabbix

ll.get_logger().warning('Deprecated module, please use the new module in the same directory.')


class ZabbixProblemsToRedis(Thread):

    def __init__(
            self,
    ):
        super().__init__()
        self._health_check = 1

    def health_check(self):
        """
        health check
        """
        return self._health_check

    def run(self):
        """

        Returns:

        """
        ll.get_logger().info('Launch zabbix problems to redis service.')
        while True:
            try:
                problems = self._load_problems()
                self._send_problems_to_redis(problems)
                self._health_check = 1
            except Exception as e:
                self._health_check = 0
                ll.get_logger().error(e)
            time.sleep(60)

    @staticmethod
    def _send_problems_to_redis(problems):
        """
        send problems to redis.
        Args:
            problems:

        Returns:

        """
        RedisProblemsPublishersManager.publish(
            key='internet_zabbix',
            content=problems
        )

    @staticmethod
    def _load_problems():
        """
        load problems
        Returns:

        """
        problems_lst = LoadProblemsFromZabbix().load_problems()
        return problems_lst
